/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    2010 year
 *    Author: Michael Khromov aka MikeDM (Mike D'Morto)
 *    E-Mail: mikedmorto@gmail.com
 */
#include "maindialog.h"
#include <QDebug>

MainDialog::MainDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MainDialog)
{
    ui->setupUi(this);
    this->ui->lbl_logo->setPixmap(QPixmap("../share/pixmaps/torisakura_login_logo.png"));


}

MainDialog::~MainDialog()
{
    delete ui;
}

void MainDialog::setInfo(const QString &text){
    this->ui->lbl_info->setText(text);
    this->setWindowTitle(APP_NAME+tr(" - standart login"));
    this->ui->stackedWidget->setCurrentIndex(0);
}
void MainDialog::slotOnBtnLogin(){
    // поймали логон. порядок действий.
    // блокируем гуй.посылаем сигнал запроса функции logon.
    this->ui->stackedWidget->setCurrentIndex(1);
    this->ui->lbl_wait->setText("EXEC QUERY ....");
    this->ui->btn_retry->hide();
    QList<QVariant> param;
    param.append(this->ui->le_login->text());
    param.append(this->ui->le_pass->text());
    emit this->sigQexec(param);
}

void MainDialog::autologon(bool ret){
    if(ret){
        emit this->accept();
    }
}
void MainDialog::slotLogon(bool ret){
    // получаем что же вернулось от запроса.
    if(!ret){
        // хренова, логин неверный или еще какая беда.
        this->ui->lbl_wait->setText("Logon failure.");
        this->ui->btn_retry->show();
    }else{
        // все гуд, аццептируемся гг
        emit this->accept();
    }
}

void MainDialog::errorDb(const QString & errtext){
    // произошла критическая ошибка при работе с базой.
    this->ui->lbl_wait->setText("Logon failure:\n Database error.\n"+errtext+"\nContact your administrator");
    this->ui->btn_retry->show();
}

void MainDialog::slotRetry(){
    this->ui->le_pass->setText("");
    this->ui->stackedWidget->setCurrentIndex(0);
}

QString MainDialog::getUsername(){
    return this->ui->le_login->text();

}
